#include <stdio.h>
#include <gmp.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>

int main(int argc, char **argv) {
  int count = 0;
  mpz_t powers[512];

  unsigned long d;
  unsigned long n;

  FILE *black_hole = fopen("/dev/null", "w");
  assert(black_hole != NULL);

  for (d = 1; d < 10; d++) {
    mpz_t power;

    mpz_init(power);
    mpz_set_ui(power, 1UL);

    for (n = 1; n < 23; n++) {
      mpz_mul_ui(power, power, d);

      if (mpz_out_str(black_hole, 10, power) == n) {
        fprintf(stderr, "found that %lu ^ %lu = ", d, n);
        mpz_out_str(stderr, 10, power);
        fprintf(stderr, "\n");

        mpz_set(powers[count], power);
        count++;
      }
    }
  }

  int i;
  int j;
  for (i = 0; i < count; i++) {
    for (j = i+1; j < count; j++) {
      if (mpz_cmp(powers[i], powers[j]) == 0) {
        fprintf(stderr, "found duplicates\n");
      }
    }
  }

  printf("counted %d n-th powers\n", count);

  return 0;
}
